package com.itheima.web.servlet;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 查询全部
 */
@WebServlet("/userListServlet")
public class UserListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 调用service查询用户集合
        UserService service = new UserServiceImpl();
        List<User> userList = service.findAll();

        //2. 将数据存入request域中
        request.setAttribute("list", userList);

        //3. 转发到list.jsp 显示数据
        //request.getRequestDispatcher("/list.jsp").forward(request, response);
        //重定向防止重复提交数据
        //response.sendRedirect(request.getContextPath()+"/list.jsp");

        //查询使用转发
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
